package wei.xinya.springwebflux.service;

import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import wei.xinya.springwebflux.entity.User;

import java.util.HashMap;
import java.util.Map;

@Repository
public class UserServiceImpl implements UserService {

    private final Map<Integer, User> userMap = new HashMap<>();

    @Override
    public Mono<User> getUserById(int id) {
        return Mono.justOrEmpty(this.userMap.get(id));
    }

    @Override
    public Flux<User> getAllUser() {
        return Flux.fromIterable(this.userMap.values());
    }

    @Override
    public Mono<Void> saveUser(Mono<User> userInfo) {
        return userInfo.doOnNext(user -> this.userMap.put(this.userMap.size() + 1, user)).thenEmpty(Mono.empty());
    }

    public UserServiceImpl() {
        this.userMap.put(1, new User("wxy", "man", 22));
        this.userMap.put(2, new User("xqq", "woman", 22));
    }

}
